package org.test.client.ui.component.window;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.EmptyBorder;

import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

import com.sun.awt.AWTUtilities;

public class PopupWindow extends JWindow{
//	com.sun.awt.AWTUtilities.setWindowOpacity(i18nframe, 0.6f);  
	public double diameter = 16;
	
	private static Color successColor = Icons.getColor("dff0d8");
	private static Color warningColor = Icons.getColor("f2dede");
	private static Color infoColor = Icons.getColor("d9edf7");
	private static Color whiteColor = Icons.getColor("eff3f5");

	private static Color greenFont = Icons.getColor("468847");
	private static Color redFont = Icons.getColor("b94a48");
	private static Color blueFont = Icons.getColor("3a87ad");
	private static Color whiteFont = Icons.getColor("3a3d3f");
	
	private static Color greenBorder = Icons.getColor("d6e9c6");
	private static Color redBorder = Icons.getColor("eed3d7");
	private static Color blueBorder = Icons.getColor("bce8f1");
	private static Color whiteBorder = Icons.getColor("dedede");
	
	public static Color[] colors = new Color[]{successColor,warningColor,infoColor,whiteColor};
	public static Color[] colorFont = new Color[]{greenFont,redFont,blueFont,whiteFont};
	public static Color[] borders = new Color[]{greenBorder,redBorder,blueBorder,whiteBorder};

	public final static int TYPE_SUCCESS = 0;
	public final static int TYPE_WARNING = 1;
	public final static int TYPE_INFO = 2;
	public  final static int TYPE_WHITE = 3;

	private PopupWindow thisWindow = this;
	private JPanel contentPanel = null;
	
	private JLabel label = null;
	private String info = "";
	private BufferedImage icon = null;
	private int colorIndex = 0;
	
	private int top = 20;
	private int bottom = 20;
	private int left = 20;
	private int right = 20;
	
	private Font labelFont = new Font(Font.DIALOG,Font.PLAIN,13);

	private int defalultWidth = 400;
	private int defalultHeight = 80;
	public float translucent = 1.0f;
	public boolean needBorder = true;
	
	public static PopupWindow getInstance(String info,int type){
		return new PopupWindow(info,type);
	}
	
	public static PopupWindow getInstance(String info, BufferedImage icon,int type){
		return new PopupWindow(info,icon,type);
	}
	
	private PopupWindow(String info,int c){
		this(info,null,c);
	}
	
	private PopupWindow(String info, BufferedImage icon,int c){
		this.info = info;
		this.icon = icon;
		this.colorIndex = c;
		
		contentPanel = new JPanel();
		Container contentPane = this.getContentPane();
		contentPane.add(contentPanel);

		doUIInit();

		AWTUtilities.setWindowOpacity(this,translucent);  
		AWTUtilities.setWindowShape(this, createRoundRectShape());
//		this.pack();
		this.setSize(defalultWidth, defalultHeight);
		this.setLocationRelativeTo(null);
	}

	public void doUIInit() {
		
		this.setBackground(colors[colorIndex]);
		contentPanel.setBackground(colors[colorIndex]);
		
		contentPanel.setBorder(new EmptyBorder(top,right, bottom, left));
		
		label = new JLabel();
		label.setFont(labelFont);
		label.setForeground(colorFont[colorIndex]);
		if(icon !=null ){
			Icon ico = new ImageIcon(icon);
			label.setIcon(ico);
		}
		label.setText(info);
		contentPanel.add(label);
		
		
		CusGlassPane gp = new CusGlassPane(thisWindow);
		this.setGlassPane(gp);
		gp.setVisible(true);
		this.setAlwaysOnTop(true);
	}
	
	
	public Shape createRoundRectShape(){
		
		RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, defalultWidth,
				defalultHeight, diameter, diameter);
		return rect;
	}
	
	public static void main(String[] args) {
//		getInstance("这是一个测试信息,测试popwindow 显示程度,请合理使用." , successType).setVisible(true);
		getInstance("这是一个测试信息,测试popwindow 显示程度,请合理使用." , TYPE_WARNING).setVisible(true);
//		getInstance("这是一个测试信息,测试popwindow 显示程度,请合理使用." , infoType).setVisible(true);
//		getInstance("这是一个测试信息,测试popwindow 显示程度,请合理使用." , whiteType).setVisible(true);
	}

	public int getColorIndex() {
		return colorIndex;
	}

	public void setColorIndex(int colorIndex) {
		this.colorIndex = colorIndex;
	}

	public int getDefalultWidth() {
		return defalultWidth;
	}

	public void setDefalultWidth(int defalultWidth) {
		this.defalultWidth = defalultWidth;
	}

	public int getDefalultHeight() {
		return defalultHeight;
	}

	public void setDefalultHeight(int defalultHeight) {
		this.defalultHeight = defalultHeight;
	}
	
}	

class CusGlassPane extends JComponent{
	private CusGlassPane glass = this;
	private PopupWindow window = null;
	
	public CusGlassPane(PopupWindow window){
		this.setOpaque(false);
		this.window =window;
		addNullEvent();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		Graphics2D g2d = (Graphics2D) g.create();
//		AlphaComposite alpha = AlphaComposite.SrcOver.derive(1f);
//		Composite oldC = g2d.getComposite();
//		g2d.setColor(Color.green);
//		g2d.setComposite(alpha);
//		Dimension d = window.getPreferredSize();
//		g2d.fillRect(0, 0, d.width, d.height);
		if(window.needBorder){
			Graphics2D g2d = (Graphics2D) g;
			UIUtil.setAntiAliasing(g2d, true);
			BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
			g2d.setStroke(bs1);
			g2d.setColor(window.borders[window.getColorIndex()]);
			g2d.drawRoundRect(0,0, window.getDefalultWidth()-1, window.getDefalultHeight()-1, (int)window.diameter, (int)window.diameter);
		}
	}
	
	public void addNullEvent(){
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mouseevent) {
//				window.dispose();
				UIUtil.fadeOutWindow(window);
			}
			
			@Override
			public void mouseEntered(MouseEvent mouseevent) {
				window.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
			
			@Override
			public void mouseExited(MouseEvent mouseevent) {
				window.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
		});
	}
}
